package com.situ.blog.dao.impl;

import com.situ.blog.dao.IBlogTypeDao;
import com.situ.blog.pojo.entity.BlogType;
import com.situ.blog.pojo.query.BlogTypeQuery;
import com.situ.blog.pojo.vo.BlogTypeCountVO;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class BlogTypeDaoImpl extends HibernateDaoSupport implements IBlogTypeDao {
    @Resource
    public void setMySessionFactory(SessionFactory sessionFactory){
        super.setSessionFactory(sessionFactory);
    }

    @Override
    public List<BlogType> selectByPage(BlogTypeQuery blogTypeQuery) {
        Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
        // 存放查询参数
        Map<String, Object> paramMap = new HashMap<String, Object>();
        String sql="from BlogType where 1=1 ";
        if (!StringUtils.isEmpty(blogTypeQuery.getName())){
            sql+="and name like :name ";
            paramMap.put("name", "%"+blogTypeQuery.getName()+"%");
        }
        // 起始索引
        int offset = (blogTypeQuery.getPage() - 1) * blogTypeQuery.getLimit();
        Query query = session.createQuery(sql);
        // 设置查询参数
        if (paramMap != null && !paramMap.isEmpty()) {
            for (Map.Entry<String, Object> param : paramMap.entrySet()) {
                query.setParameter(param.getKey(), param.getValue());
            }
        }
        // 从第几条记录开始查询
        query.setFirstResult(offset);
        // 一共查询多少条记录
        query.setMaxResults(blogTypeQuery.getLimit());
        return query.list();
    }

    @Override
    public Integer selectTotalCount(BlogTypeQuery blogTypeQuery) {
        Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
        // 存放查询参数
        Map<String, Object> paramMap = new HashMap<String, Object>();
        String sql="select count(*) from BlogType where 1=1 ";
        if (!StringUtils.isEmpty(blogTypeQuery.getName())){
            sql+="and name like :name ";
            paramMap.put("name", "%"+blogTypeQuery.getName()+"%");
        }
        // 起始索引
        int offset = (blogTypeQuery.getPage() - 1) * blogTypeQuery.getLimit();
        Query query = session.createQuery(sql);
        // 设置查询参数
        if (paramMap != null && !paramMap.isEmpty()) {
            for (Map.Entry<String, Object> param : paramMap.entrySet()) {
                query.setParameter(param.getKey(), param.getValue());
            }
        }
        // 获取总记录数
        List<?> countResult = query.list();
        int total = ((Number) countResult.get(0)).intValue();
        return total;
    }

    @Override
    public void deleteById(BlogType blogType) {
        getHibernateTemplate().delete(blogType);
    }

    @Override
    public void add(BlogType blogType) {
        getHibernateTemplate().save(blogType);
    }

    @Override
    public List<BlogTypeCountVO> selectBlogTypeCount() {
        Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
        String sql = "SELECT bt.name,count(*) AS `value` \n" +
                "FROM blog AS b INNER JOIN blog_type AS bt\n" +
                "ON b.type_id=bt.id\n" +
                "GROUP BY b.type_id";
        SQLQuery query = session.createSQLQuery(sql);
        query.addScalar("name", StandardBasicTypes.STRING);
        query.addScalar("value", StandardBasicTypes.INTEGER);
        List<BlogTypeCountVO> list = query.setResultTransformer(Transformers.aliasToBean(BlogTypeCountVO.class)).list();
        return list;
    }
}
